package com.vitabuybuy.tv.network

import com.library.common.net.NetUrl
import com.vitabuybuy.tv.dto.AgreementDto
import com.vitabuybuy.tv.dto.LoginInfoDto
import com.vitabuybuy.tv.dto.TokenAndPhoneDto
import rxhttp.IAwait
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse
import java.io.File

/**
 * 作者：SimGa
 * 日期：2020/12/2
 * 时间：10:33
 * 包名：com.vitabuybuy.tv.network
 */
object AuthRepository {


    fun getCode(phone: String, areaCode: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_get_phone_code)
            .add("phone", phone)
            .add("area", areaCode)
            .toResponse()
    }

    fun getCodeV2(phone: String, areaCode: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_reset_get_phone_code)
            .add("phone", phone)
            .add("area", areaCode)
            .toResponse()
    }

    fun getCodeV3(phone: String, areaCode: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_login_get_phone_code)
            .add("phone", phone)
            .add("area", areaCode)
            .toResponse()
    }

    fun checkPhoneAndCode(code: String, phone: String, areaCode: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_check_code_phone)
            .add("phone", phone)
            .add("code", code)
            .add("area", areaCode)
            .toResponse()
    }

    fun upLoadImage(path: String): IAwait<String> {
        return RxHttp.postForm(NetUrl.api_upload_image)
            .addFile("file", File(path))
            .toResponse()
    }

    fun getEmailCode(email: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_get_email_code)
            .add("email", email)
            .toResponse()
    }

    fun checkEmailAndCode(code: String, email: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_check_email_code)
            .add("email", email)
            .add("code", code)
            .toResponse()
    }

    fun register(
        area: String?,
        phone: String?,
        password: String?,
        nickname: String?,
        avatar: String?,
        email: String?,
        gender: String?,
        birthday: String?,
        regionId: Int?
    ): IAwait<TokenAndPhoneDto> {
        val user = LoginInfoDto(
            nickname, avatar, phone, email, gender, birthday,
            LoginInfoDto.RegionBean(regionId!!)
        )
        return RxHttp.postJson(NetUrl.api_register)
            .add("user", user)
            .add("area", area)
            .add("client", "app")
            .toResponse()
    }

    fun loginByPhoneAndPwd(phone: String, password: String, areaCode: String): IAwait<TokenAndPhoneDto> {
        return RxHttp.postJson(NetUrl.api_login_by_phone_pwd)
            .add("phone", phone)
            .add("password", password)
            .add("area", areaCode)
            .add("client", "app")
            .toResponse()
    }

    fun loginByPhoneAndCode(phone: String, code: String, areaCode: String): IAwait<TokenAndPhoneDto> {
        return RxHttp.postJson(NetUrl.api_login_by_phone_code)
            .add("phone", phone)
            .add("code", code)
            .add("area", areaCode)
            .add("client", "app")
            .toResponse()
    }

    fun loginByEmailAndPwd(email: String, password: String): IAwait<TokenAndPhoneDto> {
        return RxHttp.postJson(NetUrl.api_login_by_email_pwd)
            .add("email", email)
            .add("password", password)
            .add("client", "app")
            .toResponse()
    }

    fun resetPasswordByPhone(account: String?, areaCode:String?,password: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_reset_pwd_by_phone)
            .add("phone", account)
            .add("area", areaCode)
            .add("newPassword", password)
            .add("client", "app")
            .toResponse()
    }

    fun resetPasswordByEmail(account: String?, password: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_reset_pwd_by_email)
            .add("email", account)
            .add("newPassword", password)
            .add("client", "app")
            .toResponse()
    }


    fun getAgreement(id: Int?): IAwait<AgreementDto> {
        return RxHttp.postJson(NetUrl.api_get_agreement_detail)
            .add("id", id)
            .toResponse()
    }

    fun resetPhone(phone: String, areaCode: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_reset_phone)
            .add("phone", phone)
            .add("areaCode", areaCode)
            .add("client", "app")
            .toResponse()
    }
}